package com.whatever.slip.sfc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeMap;

import com.whatever.lottery.sfc.Bet;
import com.whatever.lottery.sfc.Item;
import com.whatever.lottery.sfc.Select;
import com.whatever.omr.OmrArea;
import com.whatever.omr.OmrPoint;

public class SlipSFC_ALL implements ISlipSFC {
	
	static final HashMap<Select, OmrPoint> mapSelect = new HashMap<Select, OmrPoint>();
	static final HashMap<String, OmrPoint> mapPlay = new HashMap<String, OmrPoint>();
	static final TreeMap<Integer, OmrPoint> mapMulti = new TreeMap<Integer, OmrPoint>();
	
	static {
		
		mapSelect.put(Select.SFC0, new OmrPoint(0,0));
		mapSelect.put(Select.SFC1, new OmrPoint(1,0));
		mapSelect.put(Select.SFC3, new OmrPoint(2,0));
		
		mapPlay.put("SFC", new OmrPoint(10,0));
		mapPlay.put("R9", new OmrPoint(11,0));
		
		mapMulti.put(2, new OmrPoint(11,0));
		mapMulti.put(3, new OmrPoint(10,0));
		mapMulti.put(4, new OmrPoint(9,0));
		mapMulti.put(5, new OmrPoint(8,0));
		mapMulti.put(6, new OmrPoint(7,0));
		mapMulti.put(7, new OmrPoint(6,0));
		mapMulti.put(8, new OmrPoint(5,0));
		mapMulti.put(9, new OmrPoint(4,0));
		mapMulti.put(10, new OmrPoint(3,0));
		
	}
	
	OmrArea oaAll;
	OmrArea oaHead;
	OmrArea oaPlayType;
	OmrArea oaBetType;
	OmrArea oaBet1;
	OmrArea oaBet2;
	OmrArea oaBet3;
	
	OmrArea oaMulti;
	List<OmrArea> oaMatchList = new ArrayList<OmrArea>();
	
	public SlipSFC_ALL() {
		oaAll = new OmrArea(14, 19, 0, 0);
		
		oaHead = new OmrArea(12, 1, 1, 0);
		oaHead.mark(2, 0);
		oaHead.mark(3, 0);
		oaHead.mark(4, 0);
		oaHead.mark(8, 0);
		oaHead.mark(11, 0);
		
		oaPlayType = new OmrArea(12, 1, 1, 1);
		
		oaBet1 = new OmrArea(4, 15, 9, 2);
		oaBet2 = new OmrArea(4, 15, 5, 2);
		oaBet3 = new OmrArea(4, 15, 1, 2);
		
		oaBetType = new OmrArea(1,1,0,3);
		
		oaMatchList.add(new OmrArea(3,1,1,1));
		oaMatchList.add(new OmrArea(3,1,1,2));
		oaMatchList.add(new OmrArea(3,1,1,3));
		oaMatchList.add(new OmrArea(3,1,1,4));
		oaMatchList.add(new OmrArea(3,1,1,5));
		oaMatchList.add(new OmrArea(3,1,1,6));
		oaMatchList.add(new OmrArea(3,1,1,7));
		oaMatchList.add(new OmrArea(3,1,1,8));
		oaMatchList.add(new OmrArea(3,1,1,9));
		oaMatchList.add(new OmrArea(3,1,1,10));
		oaMatchList.add(new OmrArea(3,1,1,11));
		oaMatchList.add(new OmrArea(3,1,1,12));
		oaMatchList.add(new OmrArea(3,1,1,13));
		oaMatchList.add(new OmrArea(3,1,1,14));
		
		oaMulti = new OmrArea(12,1,1,17);
	}
	
	@Override
	public String draw(Bet bet, int type) throws Exception {
		oaAll.fill(oaHead);
		
		oaPlayType.mark(mapPlay.get(bet.getPlay()));
		oaAll.fill(oaPlayType);
		
		//only use bet3
		
		for (int i=0;i<bet.getItemList().size();i++){
			Item item = bet.getItemList().get(i);
			if (item != null){
				OmrArea oaMatch = oaMatchList.get(i);
				markMatch(oaMatch, item);
				oaBet3.fill(oaMatch);
			}
		}
		
		//TODO:betType
		if (bet.getMoney()/bet.getBeishu()>1){
			oaBetType.mark(0,0);
			oaBet3.fill(oaBetType);
		}
		
		oaAll.fill(oaBet3);
		
		markMulti(oaMulti, bet);
		oaAll.fill(oaMulti);
		
		return oaAll.toString(type);
	}
	
//	private void markPassType(OmrArea oa, Bet bet){
//		if (Pass.valueOf("PASS"+bet.getPass().replace('*', '_')).equals(Pass.PASS1_1)){
//			oa.mark(3, 0);
//		}else if (bet.getPlay().equals("HH")){
//			oa.mark(8, 0);
//		}else {
//			oa.mark(5, 0);
//		}
//	}
	
	private void markMatch(OmrArea oa, Item item) throws Exception{
		for (Select select :item.getSelects()){
			if (!select.equals(Select.SFC9)){
				oa.mark(mapSelect.get(select));
			}
		}
		
	}
	
	private void markMulti(OmrArea oa, Bet bet) throws Exception{
		int beishu = bet.getBeishu();
		if (beishu==1){
			return;
		}
		
		OmrPoint opMulti = mapMulti.get(beishu);
		if (opMulti!=null){
			oa.mark(opMulti);
		}else{
			throw new Exception("无法填写倍数");
		}
		
	}
}
